package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsExhibitionsActivitiesParam;
import com.macro.mall.mapper.BmsExhibitionsActivitiesGoodsMapper;
import com.macro.mall.mapper.BmsExhibitionsActivitiesMapper;
import com.macro.mall.mapper.BmsExhibitionsActivitiesPicMapper;
import com.macro.mall.model.BmsExhibitionsActivities;
import com.macro.mall.model.BmsExhibitionsActivitiesGoods;
import com.macro.mall.model.BmsExhibitionsActivitiesPic;
import com.macro.mall.service.BmsExhibitionsActivitiesService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 观光车点位图片Service实现类 Created by macro on 2018/4/26.
 */
@Service
public class BmsExhibitionsActivitiesServiceImpl implements BmsExhibitionsActivitiesService {
    @Autowired
    private BmsExhibitionsActivitiesMapper bmsExhibitionsActivitiesMapper;
    @Autowired
    private UserUtils userUtils;

    @Autowired
    private BmsExhibitionsActivitiesPicMapper bmsExhibitionsActivitiesPicMapper;

    @Autowired
    private BmsExhibitionsActivitiesGoodsMapper bmsExhibitionsActivitiesGoodsMapper;

    /**
     * 分页查询展览活动
     * 
     * @return
     */
    @Override
    public List<BmsExhibitionsActivities> getList(BmsExhibitionsActivitiesParam bmsExhibitionsActivitiesParam) {
        PageHelper.startPage(bmsExhibitionsActivitiesParam.getPageNum(), bmsExhibitionsActivitiesParam.getPageSize());
        ;
        BmsExhibitionsActivities bmsExhibitionsActivities = new BmsExhibitionsActivities();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesParam, bmsExhibitionsActivities);
        List<BmsExhibitionsActivities> list = bmsExhibitionsActivitiesMapper.selectAll(bmsExhibitionsActivities);
        return list;
    }

    /**
     * 新增展览活动
     * 
     * @param
     * @return
     */
    @Override
    public int create(BmsExhibitionsActivitiesParam bmsExhibitionsActivitiesParam) {
        // 获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsExhibitionsActivities bmsExhibitionsActivities = new BmsExhibitionsActivities();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesParam, bmsExhibitionsActivities);
        bmsExhibitionsActivities.setCreateUser(userId);
        bmsExhibitionsActivities.setCreateTime(new Date());
        bmsExhibitionsActivities.setUpdateUser(userId);
        bmsExhibitionsActivities.setUpdateTime(new Date());

        int count = bmsExhibitionsActivitiesMapper.insert(bmsExhibitionsActivities);
        if (count > 0) {
            if (CollectionUtils.isNotEmpty(bmsExhibitionsActivitiesParam.getPicList())) {
                for (String pic : bmsExhibitionsActivitiesParam.getPicList()) {
                    BmsExhibitionsActivitiesPic bmsExhibitionsActivitiesPic = new BmsExhibitionsActivitiesPic();
                    // 展览活动id
                    bmsExhibitionsActivitiesPic.setExhibitionsActivitiesId(bmsExhibitionsActivities.getId());
                    // 图片地址
                    bmsExhibitionsActivitiesPic.setPic(pic);
                    bmsExhibitionsActivitiesPicMapper.insert(bmsExhibitionsActivitiesPic);
                }
            }
        }
        return count;
    }

    /**
     * 更新展览活动
     * 
     * @param
     * @return
     */
    @Override
    public int update(BmsExhibitionsActivitiesParam bmsExhibitionsActivitiesParam) {
        // 获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsExhibitionsActivities bmsExhibitionsActivities = new BmsExhibitionsActivities();
        BeanUtils.copyProperties(bmsExhibitionsActivitiesParam, bmsExhibitionsActivities);
        bmsExhibitionsActivities.setUpdateUser(userId);
        bmsExhibitionsActivities.setUpdateTime(new Date());
        // 更新展览活动
        int count = bmsExhibitionsActivitiesMapper.update(bmsExhibitionsActivities);
        if (count > 0) {
            // 删除展览活动实景图表
            List<Long> id = new ArrayList<>();
            id.add(bmsExhibitionsActivitiesParam.getId());
            bmsExhibitionsActivitiesPicMapper.delete(id);
            if (CollectionUtils.isNotEmpty(bmsExhibitionsActivitiesParam.getPicList())) {
                for (String pic : bmsExhibitionsActivitiesParam.getPicList()) {
                    BmsExhibitionsActivitiesPic bmsExhibitionsActivitiesPic = new BmsExhibitionsActivitiesPic();
                    // 展览活动id
                    bmsExhibitionsActivitiesPic.setExhibitionsActivitiesId(bmsExhibitionsActivities.getId());
                    // 图片地址
                    bmsExhibitionsActivitiesPic.setPic(pic);
                    bmsExhibitionsActivitiesPicMapper.insert(bmsExhibitionsActivitiesPic);
                }
            }
        }
        return count;
    }

    /**
     * 获取展览活动信息
     * 
     * @param id
     * @return
     */
    @Override
    public BmsExhibitionsActivities getItem(Long id) {
        BmsExhibitionsActivities entity = bmsExhibitionsActivitiesMapper.selectById(id);
        if (entity == null) {
            return new BmsExhibitionsActivities();
        }
        List<String> picList = bmsExhibitionsActivitiesPicMapper.findByExhibitionsActivitiesId(entity.getId());
        entity.setPicList(picList);
        return entity;
    }

    /**
     * 删除展览活动
     * 
     * @param ids
     * @return 完成
     */
    @Override
    public int delete(List<Long> ids) {
        List<BmsExhibitionsActivitiesGoods> goodsList = bmsExhibitionsActivitiesGoodsMapper
                .findListByExhibitionsActivitiesIds(ids);
        if (CollectionUtils.isNotEmpty(goodsList)) {
            throw new RuntimeException("展览活动存在展品未删除，请先删除展品后再删除");
        }
        bmsExhibitionsActivitiesPicMapper.delete(ids);
        return bmsExhibitionsActivitiesMapper.delete(ids);
    }
}
